﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using NCT.Framework.Database;
using NCT.Framework;
using System.Data;

namespace VISS.API.Users {

    class UserReportBC : IUserReport, IUserReportAdmin {

        #region IUserReport Members

        public bool Insert(UserReportEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Normalize data
                item.Status = EUserReportStatus.UnRead;
                item.DateCreated = DateTime.Now;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                //Insert to DB
                result = UserReportDA.Insert(item, connectionName, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                // Insert into Cache
                if (ConfigInfo.UseCache && result) {
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserReportBC.Insert({0}): \n" + ex.ToString(), SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        #endregion

        #region IUserReportAdmin Members

        public bool Delete(long iD) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                //Delete from DB
                result = UserReportDA.Delete(iD, connectionName, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                // Delete from Cache
                if (ConfigInfo.UseCache && result) {
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserReportBC.Delete({0}): \n" + ex.ToString(), iD));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        public List<UserReportEnt> GetAllByStatus(EUserReportStatus status) {
            List<UserReportEnt> result = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                if (ConfigInfo.UseCache) {
                }
                else {
                    result = UserReportDA.GetAllByStatus(status, connectionName, null, null);
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserReportBC.GetAllByStatus({0}): \n" + ex.ToString(), status));
            }
            return result;
        }

        public List<UserReportEnt> GetByReportedUserKey(string reportedUserKey) {
            List<UserReportEnt> result = null;
            try {
                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                if (ConfigInfo.UseCache) {
                }
                else {
                    result = UserReportDA.GetByReportedUserKey(reportedUserKey, connectionName, null, null);
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserReportBC.GetByReportedUserKey({0}): \n" + ex.ToString(), reportedUserKey));
            }
            return result;
        }

        #endregion
    }
}